package android.crosstime.hes;

import android.R.color;
import android.content.Context;
import android.crosstime.unit.HPoint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import edu.android.hes.R;

public class DragLayer extends RelativeLayout {
	private Context mContext = null;
//	private LayoutInflater mInflater = null;
	private ImageView mMainTab = null;
	private ImageView mSecondTab = null;
	private final static int TAB_HALF_WIDTH = 64;
	private DragOperation mDragOperation = null;

	public DragLayer(Context context){
		super(context);
		mContext = context;
//		mInflater = LayoutInflater.from(context);
		initView();
	}
	
	public DragLayer(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
//		mInflater = LayoutInflater.from(context);
		initView();
	}

	private void initView(){
		setMainTab(R.drawable.icon);
		this.setBackgroundColor(color.background_dark);
		this.addView(mMainTab);
		mMainTab.setHapticFeedbackEnabled(true);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN:
		{
			if(mMainTab == null)
				break;
			int left = (int) event.getX() - TAB_HALF_WIDTH;
			int top = (int) event.getY() - TAB_HALF_WIDTH;
//			mMainTab.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP); //buzz
			mMainTab.layout(left, top, left+TAB_HALF_WIDTH, top+TAB_HALF_WIDTH);

			if(mMainTab.getVisibility() == View.VISIBLE)
			{
				Rect rt = new Rect();
				mMainTab.getGlobalVisibleRect(rt);
				if(rt.contains( (int)event.getX(), (int) event.getY()))
				{
					//TODO: 
					
					if(mDragOperation != null)
						mDragOperation.onDragStart(new HPoint((int)event.getX(), (int)event.getY()));
				}
			}
			
		}
			break;
		case MotionEvent.ACTION_MOVE:
		{
			if(mMainTab == null)
				break;
			int left = (int) event.getX() - TAB_HALF_WIDTH;
			int top = (int) event.getY() - TAB_HALF_WIDTH;
		
			mMainTab.layout(left, top, left+TAB_HALF_WIDTH, top+TAB_HALF_WIDTH);
			if(mDragOperation != null)
				mDragOperation.onDraging(new HPoint((int)event.getX(), (int)event.getY()));
		}
			return true;
		case MotionEvent.ACTION_UP:
			if(mMainTab != null){
				mMainTab.setVisibility(View.INVISIBLE);
			}
			if(mDragOperation != null)
				mDragOperation.onDragEnd(new HPoint((int)event.getX(), (int)event.getY()));
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private void setMainTab(int resId){
		if(mMainTab == null)
			mMainTab = new ImageView(mContext);
		mMainTab.setImageResource(resId);
		mMainTab.setAlpha(128);
		mMainTab.setVisibility(View.INVISIBLE);
	}
	
	private void setSecondTab(int resId){
		if(mSecondTab == null)
			mSecondTab = new ImageView(mContext);
		mSecondTab.setImageResource(resId);
		mSecondTab.setAlpha(128);
	}
	
	public void switchDragIcon(boolean on){
		if(mMainTab != null)
			mMainTab.setVisibility(on? View.VISIBLE:View.INVISIBLE);
		this.invalidate();	
	}
	
	public void setDragListener(DragOperation dragOperation){
		mDragOperation = dragOperation;
	}
	
	interface DragOperation{
		public void onDragStart(HPoint point);
		public void onDraging(HPoint point);
		public void onDragEnd(HPoint point);
	}
	
}
